package com.i_art_engineer.meigong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.i_art_engineer.meigong.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class JigsawSelectorListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private Context mContext;

    private int viewHeight;

    private List<Integer> mImages = new ArrayList<>(); //从资源文件内加载

    public JigsawSelectorListAdapter(Context context, int height) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewHeight = height;
    }

    /**
     * 设置数据集
     *
     * @param images
     */
    public void setData(List<Integer> images) {
        if (images != null && images.size() > 0) {
            mImages = images;
        } else {
            mImages.clear();
        }
         notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mImages.size();
    }

    @Override
    public Integer getItem(int i) {
        return mImages.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.list_item_jigsaw, viewGroup, false);
            holder = new ViewHolder(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (holder != null) {
            holder.bindData(mImages.get(i));
        }

        return view;
    }

    class ViewHolder {

        ImageView image;
        View mask;

        ViewHolder(View view) {
            image = (ImageView) view.findViewById(R.id.image);
            mask = view.findViewById(R.id.mask);
            view.setTag(this);
        }

        void bindData(final Integer res) {
            if (res == null) return;
            // 处理单选和多选状态
            mask.setVisibility(View.GONE);
            // 显示图片
            Picasso.with(mContext)
                    .load(res)
                    .placeholder(R.drawable.tu)
                    .resize(viewHeight, viewHeight)
                    .centerCrop()
                    .into(image);
        }

    }

}
